#ifndef __MYSQLINIT__
#define __MYSQLINIT__
#include "threadPool.h"
MYSQL *mysqlInit(){
    char mysql_username[128] = {0};
    char mysql_psw[128] = {0};
    configureInit_mysql(mysql_username, mysql_psw);
    MYSQL * mysql = mysql_init(NULL);
    MYSQL * ret = mysql_real_connect(mysql, "localhost", mysql_username, mysql_psw, "cpp57_group16", 0, NULL, 0);
    if(ret == NULL){
        fprintf(stderr, "mysql_real_connect: %s\n", mysql_error(mysql));
        return NULL;
    }
    printf("mysql_real_connect success\n");
    return mysql;
}
MYSQL_RES *mysqlSearch(char *sql, MYSQL *mysql){
    printf("001\n");
    int qret = mysql_query(mysql, sql);
    printf("002\n");
    if(qret != 0){
        printf("003\n");
        printf("query 错误\n");
        printf("004\n");
        fprintf(stderr, "mysql_query:%s\n",mysql_error(mysql));
        printf("005\n");
    }
    printf("006\n");
    printf("mysqlSearch: sql = %s\n",sql);
    printf("007\n");
    MYSQL_RES *result = mysql_store_result(mysql);
    printf("008\n");
    if(result == NULL){
        printf("009\n");
        printf("mysql find no data\n");
        printf("010\n");
    }
    printf("011\n");
    return result;
}
int configureInit_mysql(char *user_name, char *psw){
    char str[256];
    FILE *configFile = fopen("/configure/netdisk.ini", "r");
    if (configFile == NULL)
    {
        printf("Failed to open the configuration file!\n");
        exit(-1);
    }
    for (size_t i = 0; i < 3; i++)
    {
        memset(str, 0, sizeof(str));
        fgets(str, 256, configFile);
    }
    memset(str, 0, sizeof(str));
    fgets(str, 256, configFile);
    str[strcspn(str, "\n")] = '\0';
    char *p = strstr(str, "=");
    if (p != NULL)
    {
        p++;
        sprintf(user_name, "%s", p);
        printf("user_name = %s",user_name);
    }
    else
    {
        printf("not found =\n");
    }
    memset(str, 0, sizeof(str));
    fgets(str, 256, configFile);
    str[strcspn(str, "\n")] = '\0';
    p = strstr(str, "=");
    if (p != NULL)
    {
        p++;
        sprintf(psw, "%s", p);
        printf("psw = %s",psw);
    }
    else
    {
        printf("not found =\n");
    }
}
#endif
